package user;

import book.Book;
import book.Library;
import book.PairOfUidAndBookId;
import constant.Constant;
import utils.AnalyzingBorrowedBook;
import utils.ScannerSingleton;

import java.io.IOException;
import java.util.Scanner;

public class NormalUser extends User {

    public Scanner scanner;


    public Library library = Library.getLibrary();

    //⽤⼾已借阅的图书相关信息
    private PairOfUidAndBookId[] pairOfUidAndBookIds;



    // 当前书籍的借阅量
    private int borrowedCount;

    private final AnalyzingBorrowedBook analyzingBorrowedBook = new AnalyzingBorrowedBook();
    public NormalUser(String name, int userId) {
        super(name, userId, "普通用户");
        loadBorrowedBook();
        scanner = ScannerSingleton.getScannerSingleton();

    }

    private void loadBorrowedBook(){
        PairOfUidAndBookId[] allBorrowedBook;
        try {
            //1.先加载⽂件当中的借阅信息
            allBorrowedBook= analyzingBorrowedBook.loadObject(Constant.BORROWED_BOOK_FILE_NAME);
            //2. 默认已借阅的图书数组⼤⼩为BORROW_BOOK_MAX_NUM，这⾥也可以定义到常量
            pairOfUidAndBookIds= new PairOfUidAndBookId[Constant.BORROW_BOOK_MAX_NUM];
            //3.没有读取到已借阅的图书信息
            if (allBorrowedBook== null) {
                borrowedCount = 0;
            } else {
                //4. 查看实际读取到的数组⻓度是多少？
                int allBorrowedBookLen= allBorrowedBook.length;
                //5. 如果读取到了10本书被借阅 但是当前borrowedBooks数组⻓度⼩于10
                if (allBorrowedBookLen> pairOfUidAndBookIds.length) {
                //6. 按照实际情况进⾏分配数组内存

                    pairOfUidAndBookIds = new PairOfUidAndBookId[allBorrowedBookLen];
                }
                //7.把数据拷⻉回到 已借阅图书信息的数组当中
                for (int i = 0; i < allBorrowedBookLen; i++) {
                    pairOfUidAndBookIds[i] = allBorrowedBook[i];
                }
                //8.更新当前实际借阅书籍的书籍数量
                borrowedCount = allBorrowedBookLen;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private void storeBorrowedBook() {
        try {
            analyzingBorrowedBook.storeObject(pairOfUidAndBookIds,
                    Constant.BORROWED_BOOK_FILE_NAME);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        }
    @Override
    public int display() {
        System.out.println("普通⽤⼾ " + this.getName() + " 的操作菜单:");
        System.out.println("1. 查找图书");
        System.out.println("2. 打印所有的图书");
        System.out.println("3. 退出系统");
        System.out.println("4. 借阅图书");
        System.out.println("5. 归还图书");
        System.out.println("6. 查看当前个⼈借阅情况");
        System.out.println("请选择你的操作：");
        return scanner.nextInt();
    }
    //借阅图书
    public void borrowBook() {
        //吞掉多余字符
        scanner.nextLine();
        System.out.println("请输入你要借阅的图书Id： ");
        int bookId = scanner.nextInt();
        //吞掉多余字符
        scanner.nextLine();
        //查询书架上有没有书
        if (library.bookCount == 0) {
            System.out.println("书架没有书籍暂且不能借阅");
            return;
        }
        //加载全部借阅的图书
        loadBorrowedBook();
        //遍历图书查找全部图书
        Book book = library.searchById(bookId);
        if(book == null){
            System.out.println("抱歉没有你要找的书 ");
        }
        //由pairOfUidAndBookId会传来两个id以“，”分开
        //用户id，图书id。
        for (int i = 0; i < borrowedCount; i++) {
            PairOfUidAndBookId pairOfUidAndBookId = pairOfUidAndBookIds[i];
            if(pairOfUidAndBookId.getBookId() == book.getBookId()){
                if(pairOfUidAndBookId.getUserId() == getUserId()){
                    System.out.println(bookId+"书已经被你借阅了");
                    return;
                }else {
                    System.out.println(bookId+"正在被别人借阅,他的ID是"+pairOfUidAndBookId.getUserId());
                    return;
                }
            }
        }

        library.borrowBook(bookId);

        //封装对象写到 借阅表当中
        PairOfUidAndBookId pairOfUidAndBookId = new PairOfUidAndBookId(getUserId(),
                book.getBookId());
        pairOfUidAndBookIds[borrowedCount] = pairOfUidAndBookId;
        borrowedCount++;
//存储借阅图书
        storeBorrowedBook();
        System.out.println("借阅成功！");


    }
    //归还图书
    public void returnBook() {
        loadBorrowedBook();
        if (borrowedCount == 0) {
            System.out.println("⽬前没有⽤⼾借阅过书籍");
            return;
        }
        scanner.nextLine();
        System.out.println("请输入你要归还的图书Id： ");
        int bookId = scanner.nextInt();

        scanner.nextLine();

        //查询书架上有没有书
        if (library.bookCount == 0) {
            System.out.println("书架没有书籍暂且不能借阅");
            return;
        }
        //加载全部借阅的图书
        loadBorrowedBook();
        //遍历图书查找全部图书
        Book book = library.searchById(bookId);
        if(book == null){
            System.out.println("抱歉没有你要找的书 ");
        }
        for (int i = 0; i < borrowedCount; i++) {
            PairOfUidAndBookId pairOfUidAndBookId = pairOfUidAndBookIds[i];
            if(pairOfUidAndBookIds[i].getUserId() == getUserId()){
                library.returnBook(bookId);
                if(pairOfUidAndBookIds[i].getBookId() == book.getBookId()) {
                    System.out.println(bookId + "此图书已归还");
                    pairOfUidAndBookIds[i] = pairOfUidAndBookIds[borrowedCount-1];
                    pairOfUidAndBookIds[borrowedCount - 1] = null;
                    storeBorrowedBook();
                }else {
                    System.out.println("抱歉不是你借阅的无法归还");
                }
                return;
            }
        }


    }
    // 查看个⼈借阅情况
    public void viewBorrowBooks() {
        loadBorrowedBook();
        if (borrowedCount == 0) {
            System.out.println("⽬前没有借阅过书籍");
            return;
        }else {
            for (int i = 0; i < borrowedCount; i++) {
                PairOfUidAndBookId pairOfUidAndBookId = pairOfUidAndBookIds[i];
                if(pairOfUidAndBookIds[i].getUserId() == getUserId()){
                    Book book = library.searchById(pairOfUidAndBookIds[i].getBookId());
                    System.out.println(book+book.getTitle());
                }
            }
        }

    }

}
